package per.turtle.job.controller;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.quartz.CronExpression;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.exception.job.TaskException;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.common.security.service.TokenService;
import per.turtle.job.domain.SysJob;
import per.turtle.job.service.ISysJobService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度任务信息操作处理
 *
 * @author turtle
 */
@RequestMapping("job")
@RequiredArgsConstructor
@RestController
public class SysJobController extends BaseController {

    private final ISysJobService jobService;

    private final TokenService tokenService;

    @ApiOperation(value = "新增定时任务", notes = "新增定时任务")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping("add")
    @PreAuthorize(hasPermission = "monitor:job:add")
    public CommonResult<Void> add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
        if (!CronExpression.isValidExpression(sysJob.getCronExpression())) {
            return CommonResult.error("cron表达式不正确");
        }
        sysJob.setDeptId(tokenService.getLoginUser().getSysUser().getDeptId());
        return result(jobService.insertJob(sysJob));
    }

    @ApiOperation(value = "定时任务状态修改", notes = "定时任务状态修改")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermission = "monitor:job:changeStatus")
    @PutMapping("changeStatus")
    public CommonResult<Void> changeStatus(@RequestBody SysJob job) throws SchedulerException {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setIsDisabled(job.getIsDisabled());
        return result(jobService.changeStatus(newJob));
    }

    @ApiOperation(value = "修改定时任务", notes = "修改定时任务")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermission = "monitor:job:edit")
    @PutMapping("edit")
    public CommonResult<Void> edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
        if (!CronExpression.isValidExpression(sysJob.getCronExpression())) {
            return CommonResult.error("cron表达式不正确");
        }
        return result(jobService.updateJob(sysJob));
    }

    @ApiOperation(value = "导出定时任务列表", notes = "导出定时任务列表")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    @PreAuthorize(hasPermission = "monitor:job:export")
    @ResponseExcel("定时任务")
    public List<SysJob> export(HttpServletResponse response, @RequestBody SysJob sysJob) {
        return jobService.selectJobList(sysJob);
    }

    @ApiOperation(value = "获取定时任务详细信息", notes = "获取定时任务详细信息")
    @GetMapping("{jobId}")
    @PreAuthorize(hasPermission = "monitor:job:query")
    public CommonResult<SysJob> getInfo(@PathVariable("jobId") Long jobId) {
        return CommonResult.success(jobService.selectJobById(jobId));
    }

    @ApiOperation(value = "查询定时任务列表", notes = "查询定时任务列表")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "monitor:job:list")
    public TableDataInfo<SysJob> list(@RequestBody SysJob sysJob) {
        List<SysJob> list = startPage(s -> jobService.selectJobList(sysJob));
        return getDataTable(list);
    }

    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    @DeleteMapping("{jobIds}")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "monitor:job:remove")
    public CommonResult<Void> remove(@PathVariable Long[] jobIds) throws SchedulerException {
        jobService.deleteJobByIds(jobIds);
        return CommonResult.success();
    }

    @ApiOperation(value = "定时任务立即执行一次", notes = "定时任务立即执行一次")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermission = "monitor:job:changeStatus")
    @PutMapping("run")
    public CommonResult<Void> run(@RequestBody SysJob job) throws SchedulerException {
        jobService.run(job);
        return CommonResult.success();
    }

}
